import 'dart:io';

import 'package:earclean/app/modules/splash/views/service_terms_widget.dart';
import 'package:earclean/app/request/request_main.dart';
import 'package:earclean/app/routes/app_pages.dart';
import 'package:earclean/app/util/common_utils.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:shanyan/shanYanResult.dart';
import 'package:shanyan/shanyan.dart';

class SplashController extends GetxController {
  void appStartPingBack() async {
    bool isFirst = box.read("isFirst") ?? true;
    if (isFirst) {
      Get.dialog(
              const AlertDialog(
                  shape: RoundedRectangleBorder(
                      borderRadius: BorderRadius.all(Radius.circular(12))),
                  clipBehavior: Clip.antiAlias,
                  contentPadding: EdgeInsets.zero,
                  backgroundColor: Colors.transparent,
                  elevation: 0,
                  content: ServiceTermsWidget()),
              // barrierColor: const Color(0xFF1C2023),
              barrierDismissible: false)
          .then((value) async {
        if (value == 1) {
          box.write("isFirst", false);
          // await initSy();
          await loadSet();
          Future.delayed(const Duration(seconds: 1),
              () => {Get.offAllNamed(Routes.TABLE)});
        } else if (value == 0) {
          exit(0);
        }
      });
    } else {
      // initSy();
      Future.delayed(
          const Duration(seconds: 1), () => {Get.offAllNamed(Routes.TABLE)});
    }
  }

  @override
  void onInit() {
    super.onInit();
  }

  @override
  void onReady() {
    Future.delayed(const Duration(seconds: 2), () => {appStartPingBack()});

    super.onReady();
  }

  initSy() async {
    // 初始化一键登录测试 com.253.QuanJu11 aj9SzHra
    // 初始化一键登 com.bomeng.nightmam 9vcKFnUr
    String appId = "aj9SzHra";
    // String appId = "9vcKFnUr";
    //
    ShanYanResult value = await OneKeyLoginManager().init(appId: appId);
    if (value.code == 1000) {
      ShanYanResult value1 = await OneKeyLoginManager().getPhoneInfo();
      if (value1.code == 1000) {
        box.write('oneKeyInit', true);
      } else {
        box.write('oneKeyInit', false);
      }
    } else {
      box.write('oneKeyInit', false);
    }
  }

  loadSet() async {
    // 获取设置
    Map setData = await RequestMain.getUrl({},
        turl: 'https://fjbamz.jiliol.net/sign/?a=my');
    if (setData.isNotEmpty && setData['code'].toString() == "200") {
      if (setData['data'] is Map) {
        Map data = setData['data'];
        // 已满文案(为空可预约)
        box.write("full", data['full'] ?? "");
        // 应用市场好评弹窗 0:不弹,1:弹
        box.write("rank", data['rank'].toString());
        // 默认选择的tabbar
        box.write("tab", int.parse((data['tab'] ?? "0").toString()));
        // 默认进去登录页 0  不先进登录 1  先进登录
        box.write("prelogin", data['prelogin'].toString());
        // 原生弹窗(重要)
        box.write("box", data['box'] ?? []);
        // 客服电话或邮箱
        box.write("kefu", data['kefu'] ?? '');
        // 判断接口中的quan，如果=1，直接跳转到接口中的find_url的h5页面，全屏展示，=0，还是当前的状态即可
        box.write("quan", data['quan'].toString());
        box.write("find_url", data['find_url'].toString());
        // 我的订单
        box.write("my_need_url", data['my_need_url'].toString());
        // 我的钱包
        box.write("my_account", data['my_account'].toString());
        // 消息
        box.write("chat_url", data['chat_url'].toString());
        // 客服
        box.write("kefu", data['kefu'].trim());
        // 支付方式
        box.write("pconfs", data['pconfs'] ?? []);
      }
    }
    // appStartPingBack();
  }

  @override
  void onClose() {}
}
